//
//  DynamicsViewController.m
//  JianChaRiBao
//
//  Created by 常兴宇 on 13-11-4.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "DynamicsViewController.h"
#import "DynamicsCell.h"
#import "NewspaperSingleArticleViewController.h"
#import "AppDelegate.h"
@interface DynamicsViewController ()
@property (nonatomic, retain) NSArray * mDataArray;
@end

@implementation DynamicsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self getData];
}
-(void)getData{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        id obj = [[DataRequest sharedRequest] getDynamicsHomepage];
        if ([obj isKindOfClass:[NSDictionary class]]) {
            self.mDataArray = [obj objectForKey:@"result"];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [dynamicsTableView reloadData];
            int idSum = 0;
            for (NSDictionary *dic in self.mDataArray) {
                idSum += [dic[@"public_opinion_id"] intValue];
            }
            [[NSUserDefaults standardUserDefaults] setInteger:idSum forKey:@"public_opinion_id"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        });
    });

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _mDataArray.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * cellID = @"cellID";
    DynamicsCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"DynamicsCell" owner:self options:0]lastObject];
    }
    [cell updateDisplay:[_mDataArray objectAtIndex:indexPath.row]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70.0f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NewspaperSingleArticleViewController * newspaperSingleArticleViewController = [[[NewspaperSingleArticleViewController alloc]initWithNibName:@"NewspaperSingleArticleViewController" bundle:nil]autorelease];
    newspaperSingleArticleViewController.baseInfo = [_mDataArray objectAtIndex:indexPath.row];
    newspaperSingleArticleViewController.isDynamics = YES;
    newspaperSingleArticleViewController.contentArray = self.mDataArray;
    newspaperSingleArticleViewController.currentIndex = indexPath.row;
    [[UIApplication rootNavController] pushViewController:newspaperSingleArticleViewController animated:YES];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [dynamicsTableView release];
    [super dealloc];
}
- (void)viewDidUnload {
    [dynamicsTableView release];
    dynamicsTableView = nil;
    [super viewDidUnload];
}
@end
